﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Cn.Cellula.App.Lib.MiniTop;
using System.Reflection;

namespace Cn.Cellula.MiniTop {
    public partial class FrmSetting : Form {
        public static Common.Logging.ILog log = Common.Logging.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        MySetting mset = MySetting.CreateInstance();
        public static FrmSetting frmInstance = null; 
        private FrmSetting() {
            InitializeComponent();
        }

        public static FrmSetting GetInstance() {
            if (frmInstance == null) frmInstance = new FrmSetting();
            return frmInstance; 
        }

        private void FrmSetting_Load(object sender, EventArgs e) {
            InitForm();
        }

        private void InitForm() {

            #region 基础信息
            this.txt_ThisTitle.Text = mset.Get(AppKey.FormTitle);
            if (String.IsNullOrEmpty(this.txt_ThisTitle.Text)) {
                this.txt_ThisTitle.Text = MySetting.FORM_TITLE;
            }
            #endregion

            #region 页面相关
            this.txt_Website.Text = mset.Get(AppKey.HostAddress);
            if (String.IsNullOrEmpty(this.txt_Website.Text)) {
                this.txt_Website.Text = MySetting.HOST_ADDRESS;
            }
            this.txt_LoginPage.Text = mset.Get(AppKey.LoginPage);
            if(String.IsNullOrEmpty(this.txt_LoginPage.Text)){
                this.txt_LoginPage.Text = "Port/MiniTopLogin.aspx";
            }
            this.txt_LogoutPage.Text = mset.Get(AppKey.LogoutPage);
            if (String.IsNullOrEmpty(this.txt_LogoutPage.Text)) {
                this.txt_LogoutPage.Text = "Port/MiniTopLogin.aspx";
            }
            this.txt_HelpPage.Text = mset.Get(AppKey.HelpPage);

            this.txt_MasterSite.Text = mset.Get(AppKey.MasterAddress);
            if (String.IsNullOrEmpty(this.txt_MasterSite.Text)) {
                this.txt_MasterSite.Text = MySetting.MASTER_ADDRESS;
            }
            #endregion

            #region 显示相关
            this.cb_AutoHide.Checked = (mset.Get(AppKey.AutoHide) == "yes");
            //初始化宽度
            String width = mset.Get(AppKey.MainFormWidth);
            if (String.IsNullOrEmpty(width)) {
                this.nud_Width.Value = FrmDefault.GetInstance().Width;
            } else {
                try {
                    this.nud_Width.Value = Convert.ToInt32(width);
                } catch (Exception ex) {
                    log.Error(ex.Message);
                }
            }
            //初始化高度
            String height = mset.Get(AppKey.MainFormHeight);
            if (String.IsNullOrEmpty(height)) {
                this.nud_Height.Value = FrmDefault.GetInstance().Height;
            } else {
                try {
                    this.nud_Height.Value = Convert.ToInt32(height);
                } catch (Exception ex) {
                    log.Error(ex.Message);
                }
            }
            #endregion

            #region 提醒相关
            this.cb_AlertTimerEanbeld.Checked = (mset.Get(AppKey.AlertTimerEanbled)=="yes");
            try {
                this.nud_AlertTimer.Value = Convert.ToInt32(mset.Get(AppKey.AlertTimerMinutes));
            } catch { }
            this.txt_InvokeJs.Text = mset.Get(AppKey.AlertInvokeJs);
            this.txt_JsArgs.Text = mset.Get(AppKey.AlertInvokeJsArgs);
            #endregion

            #region 高级
            this.cb_AutoResize.Checked = (mset.Get(AppKey.AutoFixFormSize) != "no");
            this.cb_autorun.Checked = (mset.Get(AppKey.AutoRun) != "no");
            this.cb_DisableScroll.Checked = (mset.Get(AppKey.EnableScroll) != "yes");
            this.cb_OneInstance.Checked = (mset.Get(AppKey.OneInstance) != "no");
            this.cb_top.Checked = (mset.Get(AppKey.OnTop) != "no");
            #endregion

            #region Email
            //this.txt_EmailAlertSing.Text = mset.Get(AppKey.EmailAlertRing);
            //this.txt_EmailClientTool.Text = mset.Get(AppKey.EmailClientTool);
            //this.txt_EmailPop3Server.Text = mset.Get(AppKey.EmailPop3Server);
            //this.txt_EmailPwd.Text = "******";
            //this.txt_EmailSmtpServer.Text = mset.Get(AppKey.EmailSmtpServer);
            //this.txt_EmailUserName.Text = mset.Get(AppKey.EmailUserName);
            //this.cb_EmailAutoAlert.Checked = (mset.Get(AppKey.EmailAutoAlert)=="yes");
            //try {
            //    this.nud_EmailAlertMins.Value = Convert.ToInt32(mset.Get(AppKey.EmailAlertTimer));
            //} catch { }
            #endregion
        }

        private void btn_Save_Click(object sender, EventArgs e) {
            IList<KeyValuePair<String, String>> list = new List<KeyValuePair<String, String>>();

            #region 基础信息
            if (String.IsNullOrEmpty(this.txt_ThisTitle.Text)) {
                MessageBox.Show("请输入名称", "INFO", MessageBoxButtons.OK);
                return;
            }
            list.Add(new KeyValuePair<string, string>(AppKey.FormTitle, this.txt_ThisTitle.Text));
            #endregion

            #region 网站信息相关
            //网站地址
            if (String.IsNullOrEmpty(this.txt_Website.Text)) {
                MessageBox.Show("请输入网站地址", "INFO", MessageBoxButtons.OK);
                return;
            }
            String hostAddr = this.txt_Website.Text;
            if(hostAddr[hostAddr.Length-1]!='/'){
                hostAddr += "/";
            }
            list.Add(new KeyValuePair<string, string>(AppKey.HostAddress, hostAddr));

            //登录页
            if (String.IsNullOrEmpty(this.txt_LoginPage.Text)) {
                MessageBox.Show("请输入登录页面", "INFO", MessageBoxButtons.OK);
                return;
            }
            list.Add(new KeyValuePair<string, string>(AppKey.LoginPage, this.txt_LoginPage.Text));
            //登录页
            if (String.IsNullOrEmpty(this.txt_LoginPage.Text)) {
                MessageBox.Show("请输入注销页面", "INFO", MessageBoxButtons.OK);
                return;
            }
            list.Add(new KeyValuePair<string, string>(AppKey.LogoutPage, this.txt_LogoutPage.Text));
            //主站地址
            list.Add(new KeyValuePair<string, string>(AppKey.MasterAddress, this.txt_MasterSite.Text));
            #endregion

            #region 显示相关
            //自适应大小
            if (this.cb_AutoResize.Checked) {
                list.Add(new KeyValuePair<string, string>(AppKey.AutoFixFormSize, "yes"));
                list.Add(new KeyValuePair<string,string>(AppKey.MainFormWidth,this.nud_Width.Value+""));
                list.Add(new KeyValuePair<string,string>(AppKey.MainFormHeight,this.nud_Height.Value+""));
            } else {
                list.Add(new KeyValuePair<string, string>(AppKey.AutoFixFormSize, "no"));
            }

            if (this.cb_DisableScroll.Checked) {
                list.Add(new KeyValuePair<string, string>(AppKey.EnableScroll, "no"));
            } else {
                list.Add(new KeyValuePair<string, string>(AppKey.EnableScroll, "yes"));
            }


            #endregion

            #region 提醒相关
            if (this.cb_AlertTimerEanbeld.Checked) {
                list.Add(new KeyValuePair<string, string>(AppKey.AlertTimerEanbled, "yes"));
                list.Add(new KeyValuePair<string, string>(AppKey.AlertTimerMinutes, this.nud_AlertTimer.Value + ""));
                list.Add(new KeyValuePair<string, string>(AppKey.AlertInvokeJs, this.txt_InvokeJs.Text));
                list.Add(new KeyValuePair<string, string>(AppKey.AlertInvokeJsArgs, this.txt_JsArgs.Text));
            } else {
                list.Add(new KeyValuePair<string, string>(AppKey.AutoFixFormSize, "no"));
            }
            #endregion

            #region 高级设置
            //单实例
            if (this.cb_OneInstance.Checked) {
                list.Add(new KeyValuePair<string, string>(AppKey.OneInstance, "yes"));
            } else {
                list.Add(new KeyValuePair<string, string>(AppKey.OneInstance, "no"));
            }
            //自动运行
            if (this.cb_autorun.Checked != (mset.Get(AppKey.AutoRun) == "yes")) {
                if (this.cb_autorun.Checked) {
                    Boolean bk = mset.RegistRunOnWindowsStart(this.GetType().Assembly.Location);
                    if(bk)
                        list.Add(new KeyValuePair<string, string>(AppKey.AutoRun, "yes"));
                } else {
                    Boolean bk = mset.UnRegistRunOnWindowsStart(this.GetType().Assembly.Location);
                    if(bk)
                        list.Add(new KeyValuePair<string, string>(AppKey.AutoRun, "no"));
                }
            }
            //启动后自动隐藏到托盘
            if (this.cb_AutoHide.Checked) {
                list.Add(new KeyValuePair<string, string>(AppKey.AutoHide, "yes"));
            } else {
                list.Add(new KeyValuePair<string, string>(AppKey.AutoHide, "no"));
            }
            if (this.cb_top.Checked) {
                list.Add(new KeyValuePair<string, string>(AppKey.OnTop, "yes"));
            } else {
                list.Add(new KeyValuePair<string, string>(AppKey.OnTop, "no"));
            }

            #endregion

            #region Email相关
            //mset.Set(AppKey.EmailAlertRing, this.txt_EmailAlertSing.Text);
            //mset.Set(AppKey.EmailAlertTimer, this.nud_EmailAlertMins.Value+"");
            //if (this.cb_EmailAutoAlert.Checked) {
            //    mset.Set(AppKey.EmailAutoAlert, "yes");
            //} else {
            //    mset.Set(AppKey.EmailAutoAlert, "no");
            //}
            //mset.Set(AppKey.EmailClientTool, this.txt_EmailClientTool.Text);
            //mset.Set(AppKey.EmailPop3Server, this.txt_EmailPop3Server.Text);
            //mset.Set(AppKey.EmailPwd, MySetting.PwdMark(this.txt_EmailPwd.Text));
            //mset.Set(AppKey.EmailSmtpServer, this.txt_EmailSmtpServer.Text);
            //mset.Set(AppKey.EmailUserName, this.txt_EmailUserName.Text);

            #endregion

            mset.Set(list);
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void cb_AutoResize_CheckedChanged(object sender, EventArgs e) {
            this.nud_Width.Enabled = this.nud_Height.Enabled = (!this.cb_AutoResize.Checked);
        }

        private void cb_AlertTimerEanbeld_CheckedChanged(object sender, EventArgs e) {
            this.nud_AlertTimer.Enabled = this.txt_InvokeJs.Enabled = this.txt_JsArgs.Enabled = this.cb_AlertTimerEanbeld.Checked;
        
}
    }
}
